const {
    RedisPrefix
} = require('./consts');

const time = require('../../../util/time');

//保存用户的单日视频观看记录
exports.markUserVideoCache = function (uid) {
    let redis = this.app.get("redis");
    let key = RedisPrefix.USER_VIDEO_MARK + uid + '_' + time.dateFormat("YYYY_mm_dd", new Date());

    return new Promise((resolve, reject) => {
        redis.exists(key, function (err, data) {
            if (err) return reject(err);
            if (data != 1) {
                redis.set(key, true, function (err, data) {
                    if (err) return reject(err);
                    redis.expire(key, 86400, function (err, data) {
                        if (err) return reject(err);
                        resolve();
                    });
                });
            } else {
                resolve();
            }
        });
    });
}

//判断用户视频观看标识是否存在
exports.videoMarkExistsCache = function (uid) {
    let redis = this.app.get("redis");
    let key = RedisPrefix.USER_VIDEO_MARK + uid + '_' + time.dateFormat("YYYY_mm_dd", new Date());
    return new Promise((resolve, reject) => {
        redis.exists(key, function (err, data) {
            if (err) return reject(err);
            if (data != 1) {
                resolve(false);
            } else {
                resolve(true);
            }
        });
    });
}